/************************************************************************
This file is part of Polar4linux
Copyright (C) 2010-2011 Eric jrdn2
Portions Copyright (C) 2007 Matti Juvonen <mpj@iki.fi> (RS400 Tools)
Portions Copyright (C) 2006 Jani Hursti <jani.hursti@hut.fi>,
Portions Copyright (C) 2001-2003 Tom Oliveira e Silva

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
************************************************************************/

#include "polarSessionView.h"

polarSessionView::polarSessionView(polar4linuxInternals *internals0)
{
	internals=internals0;
	preview_graph=new sessionGraph(&internals->default_graph_configuration);
	setupUi(this);
	view->associate(internals);

	connect(pushButtonSave, SIGNAL(clicked()), this, SLOT(saveImage()));
	connect(pushButtonDisplayLaps, SIGNAL(clicked()), this, SLOT(displayLaps()));
	connect(pushButtonDrawLaps, SIGNAL(clicked()), this, SLOT(drawLaps()));
	connect(view,SIGNAL(pageIsEmpty()),this,SLOT(hide()));
}

polarSessionView::~polarSessionView()
{
	delete preview_graph;
}

void polarSessionView::drawLaps()
{
	sessionGraph* tmpGr=dynamic_cast<sessionGraph*>(view->selectedGraph);
	if (tmpGr)
	{
		lapsGraph *Gr=new lapsGraph(tmpGr->dataNumber(0),&internals->default_graph_configuration);
		addGraph(Gr);
		plotSession();
	}
}

graph* polarSessionView::graphNumber(unsigned int i)
{
	return view->graphs[i];
}

void polarSessionView::addGraph(graph *graph)
{
	view->addGraph(graph);
}

void polarSessionView::clearPage()
{
	view->clearPage(); //removes graphs from page, and delete them
	internals->data.clearData(); //clear data in internals
}


unsigned int polarSessionView::NumberOfGraphs() const
{
	return view->graphs.size();
}

void polarSessionView::displayLaps()
{
	internals->spreadsheetW->updateExerciseList();
	internals->spreadsheetW->show();
	view->plotPage(); //plotSession();
}

void polarSessionView::closeEvent(QCloseEvent *)
{
	
	internals->spreadsheetW->invalidate();
	clearPage();
}

void polarSessionView::plotSession()
{
	//prepare data
/*
	view->clearPage();
	for (unsigned int i=0;i<view->graphs.size();i++)
	{
		view->addGraph(&view->graphs[i]);
	}
*/
	view->plotPage(); //plotSession();
}

void polarSessionView::previewHRM(hrm_data &t)
{
	preview_graph->releaseData();
	preview_graph->addData(t);
	view->plotPreview(*preview_graph);
}


void polarSessionView::saveImage()
{
	QString FilePath = QFileDialog::getSaveFileName(this,tr("Save Image"), tr("output"), tr("Image Files (*.png)"));
  if(FilePath.isEmpty()) return;
	view->saveImage(view->scene(),FilePath);
}
